% Copyright 2018  李文威 (Wen-Wei Li).
% Permission is granted to copy, distribute and/or modify this
% document under the terms of the Creative Commons
% Attribution 4.0 International (CC BY 4.0)
% http://creativecommons.org/licenses/by/4.0/

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{myarrows}[2018/02/20 Package for extendable arrows]
\RequirePackage{amsmath}
\RequirePackage{tikz-cd}

% 以下用 tikz 定义可伸缩箭头, 不用 amsmath 和 extarrows 的版本以免 unicode-math 产生问题. 代码借自 Antal Spector-Zabusky
% 重定义 \xrightarrow[below]{above}
\makeatletter
\newbox\xratbelow
\newbox\xratabove
\renewcommand{\xrightarrow}[2][]{%
	\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
	\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
	\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
	\mathrel{\tikz [->, baseline=-.75ex]
		\draw (0,0) -- node[below=-2pt] {\box\xratbelow}
		node[above] {\box\xratabove}
		(\xratlen,0) ;}}
% 重定义 \xlefttarrow[below]{above}
\renewcommand{\xleftarrow}[2][]{%
	\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
	\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
	\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
	\mathrel{\tikz [<-, baseline=-.75ex]
		\draw (0,0) -- node[below] {\box\xratbelow}
		node[above] {\box\xratabove}
		(\xratlen,0) ;}}
% 重定义 \xleftrightarrow[below]{above}
\renewcommand{\xleftrightarrow}[2][]{%
	\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
	\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
	\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
	\mathrel{\tikz [<->, baseline=-.75ex]
		\draw (0,0) -- node[below] {\box\xratbelow}
		node[above] {\box\xratabove}
		(\xratlen,0) ;}}
% 重定义 \xhookrightarrow[below]{above}, 使用 tikz-cd 的 hookrightarrow
\renewcommand{\xhookrightarrow}[2][]{%
	\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
	\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
	\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
	\mathrel{\tikz [baseline=-.75ex]
		\draw (0,0) edge[commutative diagrams/hookrightarrow] node[below] {\box\xratbelow}
		node[above] {\box\xratabove}
		(\xratlen,0) ;}}
% 重定义 \xhooklefttarrow[below]{above}, 使用 tikz-cd 的 hookleftarrow
\renewcommand{\xhookleftarrow}[2][]{%
	\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
	\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
	\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
	\mathrel{\tikz [baseline=-.75ex]
		\draw (0,0) edge[commutative diagrams/hookleftarrow] node[below] {\box\xratbelow}
		node[above] {\box\xratabove}
		(\xratlen,0) ;}}

% 重定义 \xmapsto[below]{above}, 使用 tikz-cd 的 mapsto
\renewcommand{\xmapsto}[2][]{%
	\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
	\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
	\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
	\mathrel{\tikz [baseline=-.75ex]
		\draw (0,0) edge[commutative diagrams/mapsto] node[below] {\box\xratbelow}
		node[above] {\box\xratabove}
		(\xratlen,0) ;}}

% 定义 \xlongequal[below]{above}, 使用 tikz-cd 的等号
\newcommand{\xlongequal}[2][]{%
	\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
	\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
	\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
	\mathrel{\tikz [baseline=-.75ex]
		\draw (0,0) edge[commutative diagrams/equal] node[below] {\box\xratbelow}
		node[above] {\box\xratabove}
		(\xratlen,0) ;}}
\makeatother

\endinput